package com.yijiaw.dao;

import com.yijiaw.domain.About;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.skyway.spring.util.dao.AbstractJpaDao;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

/**
 * DAO to manage About entities.
 * 
 */
@Repository("AboutDAO")
@Transactional
public class AboutDAOImpl extends AbstractJpaDao<About> implements AboutDAO {

	/**
	 * Set of entity classes managed by this DAO.  Typically a DAO manages a single entity.
	 *
	 */
	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { About.class }));

	/**
	 * EntityManager injected by Spring for persistence unit yijiaw
	 *
	 */
	@PersistenceContext(unitName = "yijiaw")
	private EntityManager entityManager;

	/**
	 * Instantiates a new AboutDAOImpl
	 *
	 */
	public AboutDAOImpl() {
		super();
	}

	/**
	 * Get the entity manager that manages persistence unit 
	 *
	 */
	public EntityManager getEntityManager() {
		return entityManager;
	}

	/**
	 * Returns the set of entity classes managed by this DAO.
	 *
	 */
	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	/**
	 * JPQL Query - findAboutById
	 *
	 */
	@Transactional
	public About findAboutById(Integer id) throws DataAccessException {

		return findAboutById(id, -1, -1);
	}

	/**
	 * JPQL Query - findAboutById
	 *
	 */

	@Transactional
	public About findAboutById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAboutById", startResult, maxRows, id);
			return (com.yijiaw.domain.About) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAboutByPrimaryKey
	 *
	 */
	@Transactional
	public About findAboutByPrimaryKey(Integer id) throws DataAccessException {

		return findAboutByPrimaryKey(id, -1, -1);
	}

	/**
	 * JPQL Query - findAboutByPrimaryKey
	 *
	 */

	@Transactional
	public About findAboutByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findAboutByPrimaryKey", startResult, maxRows, id);
			return (com.yijiaw.domain.About) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	/**
	 * JPQL Query - findAboutByContent
	 *
	 */
	@Transactional
	public Set<About> findAboutByContent(String content) throws DataAccessException {

		return findAboutByContent(content, -1, -1);
	}

	/**
	 * JPQL Query - findAboutByContent
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<About> findAboutByContent(String content, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAboutByContent", startResult, maxRows, content);
		return new LinkedHashSet<About>(query.getResultList());
	}

	/**
	 * JPQL Query - findAllAbouts
	 *
	 */
	@Transactional
	public Set<About> findAllAbouts() throws DataAccessException {

		return findAllAbouts(-1, -1);
	}

	/**
	 * JPQL Query - findAllAbouts
	 *
	 */

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<About> findAllAbouts(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllAbouts", startResult, maxRows);
		return new LinkedHashSet<About>(query.getResultList());
	}

	/**
	 * Used to determine whether or not to merge the entity or persist the entity when calling Store
	 * @see store
	 * 
	 *
	 */
	public boolean canBeMerged(About entity) {
		return true;
	}
}
